package com.myopen.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jfinal.handler.Handler;
import com.myopen.config.ConstConfig;

public class CmsHandler extends Handler{

	@Override
	public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
		
		//如果target是以/admin开头的那么全部放行，
		//如果不是,那么将 html交给/cms处理，其他静态文件交给 /cms/staticc处理
		
		
		if(target.startsWith("/admin")
				||target.startsWith("/file/upload/ckeditor")
				||target.startsWith("/file/upload")
				||target.startsWith("/file")
				||target.startsWith("/fileInfo")
				||target.startsWith("/druid")
				) {
			next.handle(target, request, response, isHandled);
		}else{
			
			if(target.endsWith(".html")) {
				request.setAttribute(ConstConfig.CMS_TARGET, target);
				next.handle("/cms", request, response, isHandled);
			}else if(target.equals("/")){
				//访问首页的时候
				request.setAttribute(ConstConfig.CMS_TARGET, target);
				next.handle("/cms", request, response, isHandled);
			}else {
				
				
				
				if(target.startsWith("/template")) {
					//此时说明，是重定向的文件，不应该再次走转发流程
					next.handle(target, request, response, isHandled);
				}else {
					
					//此处要判断是不是静态文件,如果是静态的文件的话走静态文件处理流程
					if(target.startsWith("/cms/static")
							||target.startsWith("/cms/column")
							||target.startsWith("/cms/content")
							||target.startsWith("/cms/contact")
							) {
						next.handle(target, request, response, isHandled);
					}else {
						request.setAttribute(ConstConfig.CMS_TARGET, target);
						next.handle("/cms/static", request, response, isHandled);
					}
				}
			}
		}
	}

}
